/**
 * @author zhengjiachao
 * @since 2017-4-11
 */
'use strict';
var path = require('path');

var BaseJSONCompiler = require('./adf-compiler/BaseJSONCompiler.js');

/**
 * 界面的html直接存储在设计结果的json当中，编译器直接使用这个html作为运行期要渲染的html。如果html还需要根据模型数据进行调整的话，也可以继续调整
 */

class BCPJSONCompiler extends BaseJSONCompiler{

    constructor(logger){
        super(logger);
    }

    _getBuilderEjsPath(){
        return path.join(__dirname, 'res', 'builder.ejs');
    }

    _compileHTML(json, $, document){
        super._compileHTML(json, $, document);
        var subTableContents = {};
        var subTableH5Contents = {};
        var replaceDom = function($el, $dom, setter){
            //TODO 根据模型上的值传入一些属性
            $dom.attr("adftag",$el.attr("adftag"));
            $dom.attr("adfid",$el.attr("adfid"));
            $dom.attr("ADF_Control_Type",$el.attr("ADF_Control_Type"));
            $dom.attr("style",$el.attr("style"));
            if(setter){
                setter(model);
            }
            $el.after($dom);
            $el.remove();
        }
        $('[ADF_Control_Type*="Subtable"]').each(function(index,element){
            var $el = $(element);
            var id = $el.attr('adfid');
            var model = json.cachedModel[id];
            var $dom = $('<com.beecode.bap2.biz.subTableBlock></com.beecode.bap2.biz.subTableBlock>');
            replaceDom($el, $dom);
            $dom.attr("tableAttriName",$el.attr("tableAttriName"));
            var initData = model["initData"];
            if(!initData){
                throw new Error("子表"+ id + "没有初始化数据");
            }
            var initDataJson = JSON.parse(initData);
            if(initDataJson['value']){
                subTableContents[id] = initDataJson['value'];
            }
        });
        json.subTableContents = subTableContents;
        $('[ADF_Control_Type*="SubtableH5"]').each(function(index,element){
            var $el = $(element);
            var id = $el.attr('adfid');
            var model = json.cachedModel[id];
            var $dom = $('<com.beecode.bap2.biz.h5subTableBlock></com.beecode.bap2.biz.h5subTableBlock>');
            replaceDom($el, $dom);
            var initData = model["initData"];
            if(!initData){
                throw new Error("子表"+ id + "没有初始化数据");
            }
            var initDataJson = JSON.parse(initData);
            if(initDataJson['value']){
                subTableH5Contents[id] = initDataJson['value'];
            }
        });
        json.subTableH5Contents = subTableH5Contents;
        $('[ADF_Control_Type*="Listbasedata"]').each(function(index,element){
            var $el = $(element);
            var $dom = $('<bap.dict.ListSelector></bap.dict.ListSelector>');
            replaceDom($el, $dom);
            $dom.attr("provider",$el.attr("provider"));
            $dom.attr("connection_field",$el.attr("connection_field"));
        });
    }

    _getBuilderRenderAgrs(bundleFolderPath, json, fullPath){
        var obj = super._getBuilderRenderAgrs(bundleFolderPath, json, fullPath);
        var baseInfoData = json.baseInfoData;
        var businessType = baseInfoData["billDefine"];
        if(!businessType){
            throw new Error("业务类型不能为空");
        }
        obj.businessType = businessType;
        obj.subTableContents = json.subTableContents;
        obj.subTableH5Contents = json.subTableH5Contents;
        return obj;
    }

    _getBlockEjsPath(){
        return path.join(__dirname, 'res', 'block.ejs');
    }

}

module.exports = BCPJSONCompiler;